home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2001 May / SGI Freeware 2001 May - Disc 1.iso / dist / fw_teTeX.idb / usr / freeware / bin / fmtutil.z / fmtutil
Text File  |  2001-01-10  |  14KB  |  429 lines

  1. #!/bin/sh
  2.  
  3. ###############################################################################
  4. # fmtutil: utility to maintain format files.
  5. #
  6. # Thomas Esser, (C) 1998. Public domain.
  7. #
  8. # Commands:
  9. #  --all                      recreate all format files
  10. #  --missing                  create all missing format files
  11. #  --byfmt formatname         (re)create format for `formatname'
  12. #  --byhyphen hyphenfile      (re)create formats that depend on `hyphenfile'
  13. #  --showhyphen formatname    print name of hyphenfile for format `formatname'
  14. #  --help                     show this message
  15. #
  16. # Options:
  17. #  --cnffile file             set configfile for fmtutil
  18. #  --fmtdir directory         set destination directory for format files
  19. ###############################################################################
  20.  
  21. progname=fmtutil
  22.  
  23. test -f /bin/sh5 && test -z "$RUNNING_SH5" \
  24.   && { UNAMES=`uname -s`; test "x$UNAMES" = xULTRIX; } 2>/dev/null \
  25.   && { RUNNING_SH5=true; export RUNNING_SH5; exec /bin/sh5 $0 ${1+"$@"}; }
  26. unset RUNNING_SH5
  27.  
  28. test -f /bin/bsh && test -z "$RUNNING_BSH" \
  29.   && { UNAMES=`uname -s`; test "x$UNAMES" = xAIX; } 2>/dev/null \
  30.   && { RUNNING_BSH=true; export RUNNING_BSH; exec /bin/bsh $0 ${1+"$@"}; }
  31. unset RUNNING_BSH
  32.  
  33. cnf=fmtutil.cnf   # name of the config file
  34.  
  35. ###############################################################################
  36. # setmatch(match)
  37. #   setting the "match state" to true or false. Used to see if there was at
  38. #   least one match.
  39. ###############################################################################
  40. setmatch()
  41. {
  42.   match=$1
  43. }
  44.  
  45. ###############################################################################
  46. # getmatch()
  47. #    return success if there was at least one match.
  48. ###############################################################################
  49. getmatch()
  50. {
  51.   test "x$match" = xtrue
  52. }
  53.  
  54. ###############################################################################
  55. # cache_vars()
  56. #   locate files / kpathsea variables and export variables to environment
  57. #    this speeds up future calls to e.g. mktexupd
  58. ###############################################################################
  59. cache_vars()
  60. {
  61.   : ${MT_VARTEXFONTS=`kpsewhich --expand-var='$VARTEXFONTS' | sed 's%^!!%%'`}
  62.   : ${MT_TEXMFMAIN=`kpsewhich --expand-path='$TEXMFMAIN'`}
  63.   : ${MT_MKTEXNAM=`kpsewhich --format='web2c files' mktexnam`}
  64.   : ${MT_MKTEXNAM_OPT=`kpsewhich --format='web2c files' mktexnam.opt`}
  65.   : ${MT_MKTEXDIR=`kpsewhich --format='web2c files' mktexdir`}
  66.   : ${MT_MKTEXDIR_OPT=`kpsewhich --format='web2c files' mktexdir.opt`}
  67.   : ${MT_MKTEXUPD=`kpsewhich --format='web2c files' mktexupd`}
  68.   : ${MT_MKTEX_CNF=`kpsewhich --format='web2c files' mktex.cnf`}
  69.   : ${MT_MKTEX_OPT=`kpsewhich --format='web2c files' mktex.opt`}
  70.   : ${MT_LSR_PATH=`kpsewhich  -show-path=ls-R`}
  71.   export MT_VARTEXFONTS MT_TEXMFMAIN MT_MKTEXNAM MT_MKTEXNAM_OPT MT_MKTEXDIR
  72.   export MT_MKTEXDIR_OPT MT_MKTEXUPD MT_MKTEX_CNF MT_MKTEX_OPT MT_LSR_PATH
  73. }
  74.  
  75. ###############################################################################
  76. # help()
  77. #   display help message and exit
  78. ###############################################################################
  79. help()
  80. {
  81.   cat <<'eof'
  82. Usage: fmtutil [option] ... cmd [argument]
  83.  
  84. Valid options:
  85.   --cnffile file
  86.   --fmtdir directory
  87.   --quiet                    (not implemented, just for compatibility)
  88.   --test                     (not implemented, just for compatibility)
  89.   --dolinks                  (not implemented, just for compatibility)
  90.   --force                    (not implemented, just for compatibility)
  91.  
  92. Valid commands:
  93.   --all                      recreate all format files
  94.   --missing                  create all missing format files
  95.   --byfmt formatname         (re)create format for `formatname'
  96.   --byhyphen hyphenfile      (re)create formats that depend on `hyphenfile'
  97.   --showhyphen formatname    print name of hyphenfile for format `formatname'
  98.   --edit                     edit fmtutil.cnf file
  99.   --help                     show this message
  100. eof
  101.   exit
  102. }
  103.  
  104. ###############################################################################
  105. # abort(errmsg)
  106. #   print `errmsg' to stderr and exit with error code 1
  107. ###############################################################################
  108. abort()
  109. {
  110.   echo "$progname: $1." >&2
  111.   exit 1
  112. }
  113.  
  114. ###############################################################################
  115. # main()
  116. #   parse commandline arguments, initialize variables,
  117. #   switch into temp. direcrory, execute desired command
  118. ###############################################################################
  119. main()
  120. {
  121.   destdir=     # global variable: where do we put the format files?
  122.   cnf_file=    # global variable: full name of the config file
  123.   cmd=         # desired action from command line
  124.   while
  125.     case $1 in
  126.       --cnffile)
  127.           shift; cnf_file=$1;;
  128.       --cnffile=*)
  129.           cnf_file=`echo "$1" | sed 's/--cnffile=//'`; shift ;;
  130.       --fmtdir)
  131.           shift; destdir=$1;;
  132.       --fmtdir=*)
  133.           destdir=`echo "$1" | sed 's/--fmtdir=//'`; shift ;;
  134.       --all|-a)
  135.           cmd=all;;
  136.       --edit|-e)
  137.           cmd=edit;;
  138.       --missing|-m)
  139.           cmd=missing;;
  140.       --byfmt|-f)
  141.           shift; cmd=byfmt; arg=$1;;
  142.       --byfmt=*)
  143.           cmd=byfmt; arg=`echo "$1" | sed 's/--byfmt=//'`; shift ;;
  144.       --byhyphen|-h)
  145.           shift; cmd=byhyphen; arg=$1;;
  146.       --byhyphen=*)
  147.           cmd=byhyphen; arg=`echo "$1" | sed 's/--byhyphen=//'`; shift ;;
  148.       --showhyphen|-s)
  149.           shift; cmd=showhyphen; arg=$1;;
  150.       --showhyphen=*)
  151.           cmd=showhyphen; arg=`echo "$1" | sed 's/--showhyphen=//'`; shift ;;
  152.       --help|-help)
  153.           cmd=help;;
  154.       --quiet|--test|--dolinks|--force)
  155.           ;;
  156.       "") break;;
  157.       *) echo "$progname: unknown option \`$1' ignored." >&2;;
  158.     esac
  159.   do test $# -gt 0 && shift; done
  160.  
  161.   case "$cmd" in
  162.     help|"") help;;
  163.   esac
  164.   cache_vars
  165.  
  166.   # if no cnf_file from command-line, look it up with kpsewhich:
  167.   test -z "$cnf_file" && cnf_file=`kpsewhich --format='web2c files' $cnf`
  168.   test -f "$cnf_file" || abort "config file \`$cnf' not found"
  169.  
  170.   # showhyphen and edit do not need any temp. directory, so do it here:
  171.   case "$cmd" in
  172.     showhyphen)
  173.       show_hyphen_file "$arg"
  174.       exit $?;;
  175.     edit)
  176.       ${VISUAL-${EDITOR-vi}} $cnf_file
  177.       exit;;
  178.   esac
  179.  
  180.   # set up destdir:
  181.   test -z "$MT_TEXMFMAIN" && abort "could not expand variable \$TEXMFMAIN"
  182.   if test -z "$destdir"; then
  183.     : ${VARTEXMF=`kpsewhich -expand-var='$VARTEXMF'`}
  184.     test -z "$VARTEXMF" && VARTEXMF=$MT_TEXMFMAIN
  185.     destdir=$VARTEXMF/web2c
  186.   fi
  187.   test -d "$destdir" || abort "format directory \`$destdir' does not exist"
  188.  
  189.   # find mktexupd script:
  190.   test -n "$MT_MKTEXUPD" || MT_MKTEXUPD="$TEXMFMAIN/web2c/mktexupd"
  191.  
  192.   thisdir=`pwd 2>/dev/null`
  193.   if test -n "$thisdir"; then
  194.     : ${KPSE_DOT=$thisdir}
  195.     TEXINPUTS="$thisdir:$TEXINPUTS"
  196.   fi
  197.   tmpdir=${TMP-/tmp}/$progname.$$
  198.   trap 'cd /; cd "$thisdir"; rm -rf "$tmpdir"; exit 0' 0 1 2 3 7 13 15
  199.   mkdir "$tmpdir" || abort "could not create directory \`$tmpdir'"
  200.   cd "$tmpdir" || exit 1
  201.  
  202.   # make local paths absolute:
  203.   case "$destdir" in
  204.     /*) ;;
  205.     *)  destdir="$thisdir/$destdir";;
  206.   esac
  207.   case "$cnf_file" in
  208.     /*) ;;
  209.     *)  cnf_file="$thisdir/$cnf_file";;
  210.   esac
  211.  
  212.  
  213.   # execute the desired command:
  214.   case "$cmd" in 
  215.     all)
  216.       recreate_all;;
  217.     missing)
  218.       create_missing;;
  219.     byfmt)
  220.       create_one_format "$arg";;
  221.     byhyphen)
  222.       recreate_by_hyphenfile "$arg";;
  223.   esac
  224.  
  225.   # install the log files and format files:
  226.   for i in *.log; do
  227.     test -f "$i" || continue
  228.     rm -f "$destdir/$i"
  229.     mv "$i" "$destdir/$i"
  230.   done
  231.   for i in *fmt; do
  232.     test -f "$i" || continue
  233.     rm -f "$destdir/$i"
  234.     mv "$i" "$destdir/$i" \
  235.       && echo "$progname: $destdir/$i successfully generated." >&2
  236.     $MT_MKTEXUPD "$destdir" "$i"
  237.   done
  238.   if test -f "$destdir/tex.fmt" && test ! -f "$destdir/plain.fmt"; then
  239.     ln -s tex.fmt "$destdir/plain.fmt"
  240.     $MT_MKTEXUPD "$destdir" plain.fmt
  241.   fi
  242.   if test -f "$destdir/tex.efmt" && test ! -f "$destdir/plain.efmt"; then
  243.     ln -s tex.efmt "$destdir/plain.efmt"
  244.     $MT_MKTEXUPD "$destdir" plain.efmt
  245.   fi
  246. }
  247.  
  248. ###############################################################################
  249. # parse_line(config_line) sets global variables:
  250. #   format:  name of the format, e.g. pdflatex
  251. #   engine:  name of the TeX engine, e.g. tex, etex, pdftex
  252. #   texargs: flags for initex and name of the ini file (e.g. -mltex frlatex.ini)
  253. #   fmtfile: name of the format file (without directory, but with extension)
  254. #
  255. # exit code: returns error code if the ini file is not installed
  256. ###############################################################################
  257. parse_line()
  258. {
  259.   format=$1
  260.   engine=$2
  261.   hyphenation=$3
  262.   shift; shift; shift
  263.   texargs="$@"
  264.  
  265.   eval lastarg=\$$#
  266.   inifile=`echo $lastarg | sed 's%^\*%%'`
  267.  
  268.   case "$engine" in
  269.     *etex) fmtfile="$format.efmt";;
  270.     *)     fmtfile="$format.fmt";;
  271.   esac
  272.  
  273.   # See if we can find $inifile for return code:
  274.   kpsewhich -progname=$format -format=tex $inifile >/dev/null 2>&1
  275. }
  276.  
  277. ###############################################################################
  278. # find_hyphenfile(format, hyphenation) searches for hyphenation along
  279. #                                      searchpath of format
  280. # exit code: returns error is file is not found
  281. ###############################################################################
  282. find_hyphenfile()
  283. {
  284.   format="$1"; hyphenation="$2"
  285.   kpsewhich -progname="$format" -format=tex "$hyphenation"
  286. }
  287.  
  288. ###############################################################################
  289. # find_info_for_name(format) 
  290. #   Look up the config line for format `format' and call parse_line to set
  291. #   global variables.
  292. ###############################################################################
  293. find_info_for_name()
  294. {
  295.   format="$1"
  296.  
  297.   set x `awk '$1 == format {print; exit}' format="$format" "$cnf_file"`; shift
  298.   test $# = 0 && abort "no info for format \`$format'"
  299.   parse_line "$@"
  300. }
  301.  
  302. ###############################################################################
  303. # run_initex()
  304. #   Calls initex. Assumes that global variables are set by parse_line.
  305. ###############################################################################
  306. run_initex()
  307. {
  308.   # use -efmt=xxx for e-TeX and -fmt=xxx for other engines:
  309.   case "$engine" in
  310.     *etex) fmtswitch="-efmt=$format";;
  311.     *)     fmtswitch="-fmt=$format";;
  312.   esac
  313.  
  314.   case "$format" in
  315.     cont-??) prgswitch=-progname=context;;
  316.     *)       prgswitch=-progname=$format;;
  317.   esac
  318.  
  319.   rm -f $fmtfile
  320.   $engine -ini $fmtswitch $prgswitch $texargs </dev/null
  321.   test -f $fmtfile || {
  322.     abort "\`$engine -ini $fmtswitch $prgswitch $texargs' failed"
  323.     rm -f $fmtfile
  324.   }
  325. }
  326.  
  327. ###############################################################################
  328. # recreate_loop(hyphenfile)
  329. #   for each line in config file: check match-condition and recreate format
  330. #   if there is a match
  331. ###############################################################################
  332. recreate_loop()
  333. {
  334.   OIFS=$IFS
  335.   IFS='
  336. '
  337.   set `echo x; sed '/^#/d; /^[     ]*$/d' "$cnf_file"`; shift
  338.   IFS=$OIFS
  339.   for line in "$@"; do
  340.     parse_line $line || continue
  341.     check_match || continue
  342.     echo; echo
  343.     run_initex
  344.   done
  345. }
  346.  
  347. ###############################################################################
  348. # check_match()
  349. #   recreate all formats
  350. ###############################################################################
  351. check_match()
  352. {
  353.   this_hyphenfile=`find_hyphenfile "$format" "$hyphenation"`
  354.   eval $match_cmd && setmatch true
  355. }
  356.  
  357. ###############################################################################
  358. # create_one_format(fmtname)
  359. #   (re)create the format file for the format `fmtname'
  360. ###############################################################################
  361. create_one_format()
  362. {
  363.   fmtname=$1
  364.  
  365.   find_info_for_name $fmtname || abort "format \`$fmtname' not available"
  366.   run_initex
  367. }
  368.  
  369. ###############################################################################
  370. # create_missing()
  371. #   create all missing format files
  372. ###############################################################################
  373. create_missing()
  374. {
  375.   match_cmd='test ! -f $destdir/$fmtfile'
  376.   recreate_loop
  377. }
  378.  
  379. ###############################################################################
  380. # recreate_all()
  381. #   recreate all formats
  382. ###############################################################################
  383. recreate_all()
  384. {
  385.   match_cmd=true
  386.   recreate_loop
  387. }
  388.  
  389. ###############################################################################
  390. # recreate_by_hyphenfile(hyphenfile)
  391. #   recreate all formats that depend on hyphenfile
  392. ###############################################################################
  393. recreate_by_hyphenfile()
  394. {
  395.   hyphenfile=$1
  396.  
  397.   match_cmd="test x\$this_hyphenfile = x$hyphenfile"
  398.  
  399.   # No match before the loop:
  400.   setmatch false
  401.  
  402.   recreate_loop
  403.  
  404.   # Now check if there was at least one match:
  405.   getmatch || abort "no format depends on \`$hyphenfile'"
  406. }
  407.  
  408.  
  409. ###############################################################################
  410. # show_hyphen_file(format)
  411. #   prints full name of the hyphenfile for format
  412. #
  413. # exit code: returns error code if the ini file is not installed or if
  414. #            the hyphen file cannot be found
  415. ###############################################################################
  416. show_hyphen_file()
  417. {
  418.   fmtname=$1
  419.  
  420.   find_info_for_name "$fmtname" || abort "no info for format \`$fmtname'"
  421.   if test "x$hyphenation" = x-; then
  422.     echo -
  423.     exit 0
  424.   fi
  425.   find_hyphenfile "$format" "$hyphenation" || abort "hyphenfile \`$hyphenation' not found"
  426. }
  427.  
  428. main "$@"
  429.